<script setup>
import { ref, onMounted } from 'vue'
import { queryPageApi, queryDeptListApi } from '@/api/user'
import { ElMessage } from 'element-plus'

// 查询参数
const queryParams = ref({
  name: '',
  phone: '',
  deptId: '',
  status: '',
  page: 1,
  pageSize: 10
})

// 表格数据
const userList = ref([])
const total = ref(0)

// 部门列表数据
const deptList = ref([])

// 搜索
const search = async () => {
  const result = await queryPageApi(queryParams.value)
  if (result.code === 1) {
    userList.value = result.data.rows
    total.value = result.data.total
  }
}

// 清空
const clear = () => {
  queryParams.value = { name: '', phone: '', deptId: '', status: '', page: 1, pageSize: 10 }
  search()
}

// 分页大小改变
const handleSizeChange = (val) => {
  search()
}

// 页码改变
const handleCurrentChange = (val) => {
  search()
}

// 获取部门列表
const getDeptList = async () => {
  const result = await queryDeptListApi()
  if (result.code === 1) {
    deptList.value = result.data
  }
}

// 页面加载完成
onMounted(() => {
  getDeptList()
  search()
})
</script>

<template>
  <div class="user">
    <!-- 搜索区域 -->
    <el-form :inline="true" :model="queryParams" ref="queryForm" class="search-form">
      <el-form-item label="姓名">
        <el-input v-model="queryParams.name" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="queryParams.phone" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="部门">
        <el-select v-model="queryParams.deptId" placeholder="请选择" clearable style="width: 100%">
          <el-option 
            v-for="dept in deptList" 
            :key="dept.id" 
            :label="dept.name" 
            :value="dept.id" 
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="queryParams.status" placeholder="请选择" clearable style="width: 100%">
          <el-option label="正常" :value="1" />
          <el-option label="停用" :value="0" />
        </el-select>
      </el-form-item>
      <el-form-item class="search-buttons">
        <el-button type="primary" @click="search">
          <el-icon><Search /></el-icon>搜索
        </el-button>
        <el-button @click="clear">
          <el-icon><Refresh /></el-icon>清空
        </el-button>
      </el-form-item>
    </el-form>

    <!-- 操作按钮区域 -->
    <div class="operation-area">
      <el-button type="primary">
        <el-icon><Plus /></el-icon>添加用户
      </el-button>
      <el-button>批量删除</el-button>
    </div>

    <!-- 表格区域 -->
    <div class="table-container">
      <el-table :data="userList" style="width: 100%" border>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column prop="name" label="姓名" width="120" align="center" />
      <el-table-column prop="username" label="用户名" width="120" align="center" />
      <el-table-column prop="image" label="头像" width="100" align="center">
        <template #default="scope">
          <el-avatar 
            :size="40" 
            :src="scope.row.image" 
            :alt="scope.row.name"
          >
            <el-icon><User /></el-icon>
          </el-avatar>
        </template>
      </el-table-column>
      <el-table-column prop="deptName" label="部门" width="150" align="center" />
      <el-table-column prop="roleName" label="角色" width="150" align="center" />
      <el-table-column prop="phone" label="手机号" width="150" align="center" />
      <el-table-column prop="status" label="状态" width="100" align="center">
        <template #default="scope">
          <el-tag :type="scope.row.status === 1 ? 'success' : 'danger'">
            {{ scope.row.status === 1 ? '正常' : '停用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="updateTime" label="最后操作时间" width="180" align="center" />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" link>
            <el-icon><Edit /></el-icon>修改
          </el-button>
          <el-button type="danger" link>
            <el-icon><Delete /></el-icon>删除
          </el-button>
        </template>
      </el-table-column>
      </el-table>
    </div>

    <!-- 分页区域 -->
    <div class="pagination">
      <el-pagination
        v-model:current-page="queryParams.page"
        v-model:page-size="queryParams.pageSize"
        :page-sizes="[10, 20, 30, 40, 50]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<style scoped>
.user {
  padding: 20px;
  background-color: #fff;
  height: 100%;
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.operation-area {
  margin: 20px 0;
  flex-shrink: 0;
}

.search-form {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  flex-shrink: 0;
}

.search-form :deep(.el-form-item) {
  margin-right: 20px;
  min-width: 200px;
}

.search-buttons {
  margin-left: auto !important;
  min-width: auto !important;
}

.table-container {
  flex: 1;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.el-table {
  flex: 1;
  overflow: auto;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
  flex-shrink: 0;
}

:deep(.el-button .el-icon) {
  margin-right: 4px;
}
</style>
